package cli;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Algorithms {
	
	File algorithmList = new File("./lib/mat");		//file directory location
	
	String[] algorithms;
	List<String> matlabAlgorithms = new ArrayList<String>();
	String setAlgorithm;
	
//	Scanner input = new Scanner(System.in);				//allows user input
			
	/**
	 * Constructor
	 */
	public Algorithms() {		
	}
	
	/**
	 * Retrieve the matlab algorithms in the specified directory <br>
	 * Assumes that all files ending in '.jar' are matlab algorithms
	 */
	public void obtainAlgorithmList(){
		matlabAlgorithms.clear();
		algorithms = algorithmList.list();				//adds the file list to the array
		for(int i = 0; i < algorithms.length; i++){
			if ( algorithms[i].endsWith(".jar" )) {
				String algoName = algorithms[i].substring(0, algorithms[i].lastIndexOf('.'));
				matlabAlgorithms.add( algoName );
			}
		}		
	}
	
	/**
	 * Returns the matlab algorithms list
	 * 
	 * @return list of matlab algorithms as string array
	 */
	public String[] getAlgorithmList(){					//returns the directory list
		String[] a = new String[ matlabAlgorithms.size() ];
		return matlabAlgorithms.toArray( a );								
	}
/*	
	public void setAlgorithm(){
		if(algorithms == null){
		System.out.println("There are no files stored. This may be caused by lack of files in the repository.");
		}
		else{
			System.out.println("Files in directory");
			for(int i = 0; i < algorithms.length; i++){
				String filename = algorithms[i];						//gets filename of the file
				System.out.println(i + ". " +filename);					//prints filename
			}
		}
			
		System.out.println("Which file would you like to set as default?");
		System.out.println("Please select the corresponding number");
		int selection = input.nextInt();

		while(selection < 0 || selection > algorithms.length){
			System.out.println("The given input is not a valid selection!");
			System.out.println("Please input a valid input");
			selection = input.nextInt();
		}
		
		String setAlgorithm = algorithms[selection];		//will give the string the selected filename
		
		try{
			File setDefaultAlgorithm = new File("");		//finds the file to store set algorithm
			
			if(!setDefaultAlgorithm.exists()){
				setDefaultAlgorithm.createNewFile();		//creates the file if it is not found at the set location
			}
		
			FileWriter fw = new FileWriter(setDefaultAlgorithm.getAbsoluteFile());		//gets the file that is to written to
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(setAlgorithm);														//writes the String to the file
			bw.close();																	//closes the file so nothing else is written to it

		}catch(IOException e){
			e.printStackTrace();
		}
	}
*/
	
}

